/*
 *
 * Common Validation Framework, version 0.8
 *  (c) 2006-2007 Demo2do
 *
 *  For details, see the Demo2do web site: http://www.demo2do.com/
 *
 * @author keen.jiang
 *
/*--------------------------------------------------------------------------*/

/**
 * define package name
 */
if (demo2do == undefined) var demo2do = {};

demo2do.utils = {

    isValidIp : function(ip){
        return /^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/.test(ip);
    },

    isValidNetMask : function(netmask){

        if (netmask != 0x0 && (netmask & 0x1) == 0x0)
        {
            while ((netmask & 0x80000000) != 0x0)
                netmask <<= 0x1;
            return netmask == 0x0;
        }
        return false;
    }
};

/**
 * enhancement for prototype lib
 */
Object.extend(Object, {

  isEmpty: function (value){

    if(value == undefined || value == null || value == "")
        return true;
    else
        return false;
   },

  isBlank: function (value){

    return /^\s*$/.test(value) || this.isEmpty(value);
  },

  isNumber: function(value){
        return /^-{0,1}\d*\.{0,1}\d+$/.test(value);
  },

  slice: function(value, x, y){
      if(Object.isEmpty(value)) return value;
      if(value.length<x || value.length >y) return "";
      if(y==null) return value.slice(x);
      return value.slice(x, y);
  }
});



Object.extend(String.prototype, {
  trim: function() {
   var temp = this;
    //var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   var obj = /(^\s*)|(\s*$)/g;
   if (obj.test(temp)) { temp = temp.replace(obj, ''); }
   return temp;
  }
});
